package com.tvt.sexyapp_demo.fragment.dialog;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.tvt.sexyapp_demo.R;
import com.tvt.sexyapp_demo.appconfig.xml.XItem;
import com.tvt.sexyapp_demo.appconfig.xml.XPopupConfirm;
import com.tvt.sexyapp_demo.debug.Debug;
import com.tvt.sexyapp_demo.sms.SmsSender;

public class SMSDialog extends DialogFragment {

	// ===================================================================
	// Constants
	// ===================================================================
	private final String LOG_TAG = this.getClass().getSimpleName();
	// ===================================================================
	// Fields
	// ===================================================================
	private XItem mItem;
	private XPopupConfirm mXPopupConfirm;
	
	// ===================================================================
	// Constructors
	// ===================================================================
	public SMSDialog(XItem pItem,XPopupConfirm pXPopupConfirm) {
		super();
		this.mItem = pItem;
		this.mXPopupConfirm = pXPopupConfirm;
	}
	
	// ===================================================================
	// Getter & Setter
	// ===================================================================

	// ===================================================================
	// Methods from BaseClass & Interfaces
	// ===================================================================
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = this.getDialogConfirm(inflater);
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		getDialog().getWindow().setBackgroundDrawable(
				new ColorDrawable(Color.TRANSPARENT));
		getDialog().setCanceledOnTouchOutside(false);

		TextView txtPopupTitle = (TextView) view
				.findViewById(R.id.txtPopupTitle);
		txtPopupTitle.setText(this.mXPopupConfirm.getTitle());
		
		TextView txtPopupContent = (TextView) view
				.findViewById(R.id.txtPopupContent);
		txtPopupContent.setText(this.mXPopupConfirm.getContent());
		
		this.registerEventHandler(view);
		return view;
	}

	// ===================================================================
	// Methods
	// ===================================================================

	
	private View getDialogConfirm(LayoutInflater pInflater) {
		View view = pInflater.inflate(R.layout.sms_dialog, null);
		return view;
	}

	private void registerEventHandler(View pView) {

		Button btnOK = (Button) pView.findViewById(R.id.btnPopupOk);
		btnOK.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				SmsSender.sendTextSMS(mItem.getSms());
				SMSDialog.this.redirectWebBrowser(mItem.getBrowserUrl());
				SMSDialog.this.dismiss();
			}
		});

		Button btnCancel = (Button) pView.findViewById(R.id.btnPopupCancel);
		btnCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				//SmsSender.sendTextSMS(mItem.getSms());
				SMSDialog.this.dismiss();
			}
		});
	}

	private void redirectWebBrowser(String pUrl) {

		if (pUrl != null) {
			Intent urlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pUrl));
			startActivity(urlIntent);
		} else
			Debug.e(LOG_TAG, "Can't open null url!");
	}
}
